/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.jsonarry.nanningtravel.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.support.v7.widget.RecyclerView.LayoutParams;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

public class DividerGridViewItemDecoration extends RecyclerView.ItemDecoration {

    private static final int[] ATTRS = new int[]{
            android.R.attr.listDivider
    };

    private Drawable mDivider;

    public DividerGridViewItemDecoration(Context context, int orientation) {
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent) {
            drawVertical(c, parent);
            drawHorizontal(c, parent);
    }

    public void drawVertical(Canvas c, RecyclerView parent) {

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);
            final LayoutParams params = (LayoutParams) child
                    .getLayoutParams();
            //垂直间隔线的矩形
            final int left = child.getRight() + params.rightMargin;
            final int right = left + mDivider.getIntrinsicWidth();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    public void drawHorizontal(Canvas c, RecyclerView parent) {

        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
        	View child = parent.getChildAt(i);
        	LayoutParams params = (LayoutParams) child.getLayoutParams();
        	//child离左边的距离-左外边距
        	int left = child.getLeft() - params.leftMargin;
        	int right = child.getRight() + params.rightMargin + mDivider.getIntrinsicWidth();
        	int top = child.getBottom() + params.bottomMargin;
        	int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
    	//根据itemPosition判断该格子是否是最后一行 或者 最后一列，都不应该绘制间隔线
    	int right = mDivider.getIntrinsicWidth();
    	int bottom = mDivider.getIntrinsicHeight();
    	if(isLastColum(parent,itemPosition)){//判断是否是最后一列,不要显示右边
//    		outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    		right = 0;
    	}
    	if(isLastRow(parent,itemPosition)){//判断是否是最后一行,就不要显示下边
//    		outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    		bottom = 0;
    	}
    	{
            outRect.set(0, 0, right, bottom);
    	}
    }

    //判断是否是最后一行
	private boolean isLastRow(RecyclerView parent, int itemPosition) {
		int spanCount = getSpanCount(parent);
		LayoutManager layoutManager = parent.getLayoutManager();
		if(layoutManager instanceof GridLayoutManager){
			int childCount = parent.getAdapter().getItemCount();
			int lastRowCount = childCount%spanCount;
//			if(itemPosition+spanCount>=childCount||itemPosition+1==childCount){
//				return true;
//			}
			//最后一行数量小于spanCount 或者 ==0
			if(lastRowCount<spanCount||lastRowCount==0){
				return true;
			}
		}
		return false;
	}

	private boolean isLastColum(RecyclerView parent, int itemPosition) {
		LayoutManager layoutManager = parent.getLayoutManager();
		if(layoutManager instanceof GridLayoutManager){//横竖都要判断
			if((itemPosition+1)%getSpanCount(parent)==0){//是最后一列
				return true;
			}
		}
		return false;
	}

	private int getSpanCount(RecyclerView parent) {
		int count = -1;
		LayoutManager layoutManager = parent.getLayoutManager();
		if(layoutManager instanceof GridLayoutManager){//横竖都要判断
			count  = ((GridLayoutManager)layoutManager).getSpanCount();
		}
		return count;
	}
}
